import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
from backend.P2P.device1 import devices
import sqlite3,uuid,time
def load_device():
    with sqlite3.connect("p2p.db", check_same_thread=False,timeout=10) as connect:
        cursor=connect.cursor()
        cursor.execute("select * from device")
        rows=cursor.fetchall()
        print(rows)
        devices=[]
        for row in rows:
            device={
                "id":row[0],
                "name":row[1],
                "ip":row[4],
                "online":True,
                "owner":row[3]
            }
            devices.append(device)
        print(devices)

        return devices
def save_device(id,name,type,owner_id,host,status,timestamp):
    with sqlite3.connect("p2p.db", check_same_thread=False,timeout=10) as connect:
        cursor=connect.cursor()
        timestamp = int(time.time() * 1000)
        status = 1 if status else 0
        # if 
        cursor.execute('''
        INSERT OR REPLACE INTO device (id, name, type, owner_id, host, status, timestamp)
        VALUES (?, ?, ?, ?, ?, ?, ?)
    ''', (id, name,type, owner_id, host, status, timestamp))

# (id, name, type, owner_id, host, status, timestamp)

def add_device(id):
    device=devices()
    device_infos=device.checkSupport()
    if not device_infos:
        print("[ERROR] checkSupport 返回了 None 或空值")
        return []  # 返回空列表，防止崩溃
    print(device_infos)
    result= []
    function=[]
    for device_info in device_infos:
        if device_info.get("type")=="printer":
            function=[{"command":"print","parameter":None}]     
        dev={
        "icon":'',
        "id":  f"device_123",                  
        "name": device_info.get("name"),           			 
        "online":device_info.get("status"),
        "ip": "127.0.0.1",			
        "function":function    
        }
        result.append(dev)
        save_device(dev["id"],dev["name"], device_info.get("type"),id,dev["ip"],dev["online"])
        return result
def save_device(id,name,type,owner_id,host,status):
    with sqlite3.connect("p2p.db", check_same_thread=False,timeout=10) as connect:
        cursor=connect.cursor()
        timestamp = int(time.time() * 1000)
        status = 1 if status else 0
        # if 
        cursor.execute('''
        INSERT OR REPLACE INTO device (id, name, type, owner_id, host, status, timestamp)
        VALUES (?, ?, ?, ?, ?, ?, ?)
    ''', (id, name,type, owner_id, host, status, timestamp))

# (id, name, type, owner_id, host, status, timestamp)
def enable_device(message,socketio,id,timestamp,name):
    if message=="打印":
        device=devices()
        device.print()
        message_="打印成功"
        message_request={
                    "action": "loadMessage",    
                    "friend_id":id,
                    "friend_name":name,
                    "message":message_,
                    "timestamp":timestamp
                }
        socketio.emit("client",message_request)        
    else:
        print("未识别信息")
        message_="打印失败"
        message_request={
                    "action": "loadMessage",    
                    "friend_id":id,
                    "friend_name":name,
                    "message":message_,
                    "timestamp":timestamp
                }
        socketio.emit("client",message_request)     
if __name__ == "__main__":
    # enable_device("打印")     
    # load_device()   
    # add_device(1)
    pass